package com.example.finaltool.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author dengbw
 */
@Mapper
public interface MenuMapper {

    @Select("SELECT DISTINCT s.sub_tenant_id FROM sys_menu s WHERE tenant_id = #{corpId}")
    List<Long> getMenuSubIds(String corpId);
    @Select("SELECT DISTINCT s.sub_tenant_id FROM sys_role s WHERE tenant_id = #{corpId}")
    List<Long> getRoleSubIds(String corpId);
    @Select("SELECT DISTINCT s.sub_tenant_id FROM sys_role_menu s WHERE tenant_id = #{corpId}")
    List<Long> getRoleMenuSubIds(String corpId);
    @Select("SELECT DISTINCT s.sub_tenant_id FROM sys_org s WHERE tenant_id = #{corpId} AND s.sub_tenant_id is NOT NULL")
    List<Long> getOrgSubIds(String corpId);

    @Insert("INSERT INTO sys_menu (menu_id,root_id,parent_id,NAME,path,redirect,url,perms,type,icon,order_num,plat,STATUS,tenant_id,sub_tenant_id,ext_flag,dashboard_icon,dashboard_show)SELECT sm.menu_id,sm.root_id,sm.parent_id,sm.NAME,sm.path,sm.redirect,sm.url,sm.perms,sm.type,sm.icon,sm.order_num,sm.plat,sm.STATUS,#{corpId} as tenant_id,#{subId} as sub_tenant_id,sm.ext_flag,sm.dashboard_icon,sm.dashboard_show FROM sys_menu sm WHERE sm.sub_tenant_id='503221937'")
    void saveNonghuMenu(String corpId, Long subId);
    @Insert("INSERT INTO sys_menu (menu_id,root_id,parent_id,NAME,path,redirect,url,perms,type,icon,order_num,plat,STATUS,tenant_id,sub_tenant_id,ext_flag,dashboard_icon,dashboard_show)SELECT sm.menu_id,sm.root_id,sm.parent_id,sm.NAME,sm.path,sm.redirect,sm.url,sm.perms,sm.type,sm.icon,sm.order_num,sm.plat,sm.STATUS,#{corpId} as tenant_id,#{subId} as sub_tenant_id,sm.ext_flag,sm.dashboard_icon,sm.dashboard_show FROM sys_menu sm WHERE sm.sub_tenant_id='503692436'")
    void saveGanbuMenu(String corpId, Long subId);
    @Insert("INSERT INTO sys_menu (menu_id,root_id,parent_id,NAME,path,redirect,url,perms,type,icon,order_num,plat,STATUS,tenant_id,sub_tenant_id,ext_flag,dashboard_icon,dashboard_show)SELECT sm.menu_id,sm.root_id,sm.parent_id,sm.NAME,sm.path,sm.redirect,sm.url,sm.perms,sm.type,sm.icon,sm.order_num,sm.plat,sm.STATUS,#{corpId} as tenant_id,#{subId} as sub_tenant_id,sm.ext_flag,sm.dashboard_icon,sm.dashboard_show FROM sys_menu sm WHERE sm.sub_tenant_id='503286940'")
    void saveFuwuMenu(String corpId, Long subId);
    @Insert("INSERT INTO sys_role (role_id,role_name,role_type,remark,org_guid,group_id,plat,create_user_id,create_time,tenant_id,sub_tenant_id)SELECT sm.role_id,sm.role_name,sm.role_type,sm.remark,sm.org_guid,sm.group_id,sm.plat,sm.create_user_id,sm.create_time,#{corpId} as tenant_id, #{subId} as sub_tenant_id FROM sys_role sm WHERE sm.sub_tenant_id='503692436'")
    void saveRole(String corpId, Long subId);
    @Insert("INSERT INTO sys_role_menu (id,role_id,menu_id,tenant_id,sub_tenant_id)SELECT srm.id,srm.role_id,srm.menu_id,#{corpId} as tenant_id,#{subId} as sub_tenant_id FROM sys_role_menu srm WHERE srm.sub_tenant_id=503221937")
    void saveNonghuRoleMenu(String corpId, Long subId);
    @Insert("INSERT INTO sys_role_menu (id,role_id,menu_id,tenant_id,sub_tenant_id)SELECT srm.id,srm.role_id,srm.menu_id,#{corpId} as tenant_id,#{subId} as sub_tenant_id FROM sys_role_menu srm WHERE srm.sub_tenant_id=503286940")
    void saveFuwuRoleMenu(String corpId, Long subId);
    @Insert("INSERT INTO sys_role_menu (id,role_id,menu_id,tenant_id,sub_tenant_id)SELECT srm.id,srm.role_id,srm.menu_id,#{corpId} as tenant_id,#{subId} as sub_tenant_id FROM sys_role_menu srm WHERE srm.sub_tenant_id=503692436")
    void saveGanbuRoleMenu(String corpId, Long subId);
    @Insert("INSERT INTO `sys_org`(`org_guid`, `parent_guid`, `alarm_type`, `type_id`, `org_name`, `path`, `name_path`, `org_code`, `status`, `remark`, `create_time`, `update_time`, `tenant_id`) VALUES (1, 0, '3', 0, #{name}, '1', #{name}, '0', 1, NULL, NOW(), NULL, #{corpId})")
    void saveOrg(String name, String corpId);

    @Delete("DELETE FROM sys_menu WHERE tenant_id = #{corpId}")
    void deleteMenu(String corpId);
    @Delete("DELETE FROM sys_role WHERE tenant_id = #{corpId}")
    void deleteRole(String corpId);
    @Delete("DELETE FROM sys_role_menu WHERE tenant_id = #{corpId}")
    void deleteRoleMenu(String corpId);
    @Delete("DELETE FROM sys_org WHERE tenant_id = #{corpId}")
    void deleteOrg(String corpId);

    @Delete("DELETE FROM sys_menu WHERE sub_tenant_id = #{subId}")
    void deleteCunMenu(Long subId);
    @Delete("DELETE FROM sys_role WHERE sub_tenant_id = #{subId}")
    void deleteCunRole(Long subId);
    @Delete("DELETE FROM sys_role_menu WHERE sub_tenant_id = #{subId}")
    void deleteCunRoleMenu(Long subId);
    @Delete("DELETE FROM sys_org WHERE sub_tenant_id = #{subId}")
    void deleteCunOrg(Long subId);
}
